
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 10/19/2011 23:00:42
-- Generated from EDMX file: C:\Users\rene\Documents\CodeProjects\PlCommunity\src\Forum.DataAccess\DbModel\ForumDbModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [renebjo_forum];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_ForumThread]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Threads] DROP CONSTRAINT [FK_ForumThread];
GO
IF OBJECT_ID(N'[dbo].[FK_ThreadPost]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Posts] DROP CONSTRAINT [FK_ThreadPost];
GO
IF OBJECT_ID(N'[dbo].[FK_SubForumSubForum]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[SubForums] DROP CONSTRAINT [FK_SubForumSubForum];
GO
IF OBJECT_ID(N'[dbo].[FK_UserPost]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Posts] DROP CONSTRAINT [FK_UserPost];
GO
IF OBJECT_ID(N'[dbo].[FK_UserThread]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Threads] DROP CONSTRAINT [FK_UserThread];
GO
IF OBJECT_ID(N'[dbo].[FK_CategorySubForum]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[SubForums] DROP CONSTRAINT [FK_CategorySubForum];
GO
IF OBJECT_ID(N'[dbo].[FK_CategoryThread]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Threads] DROP CONSTRAINT [FK_CategoryThread];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Threads]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Threads];
GO
IF OBJECT_ID(N'[dbo].[Posts]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Posts];
GO
IF OBJECT_ID(N'[dbo].[SubForums]', 'U') IS NOT NULL
    DROP TABLE [dbo].[SubForums];
GO
IF OBJECT_ID(N'[dbo].[Categories]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Categories];
GO
IF OBJECT_ID(N'[dbo].[Users]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Users];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Threads'
CREATE TABLE [dbo].[Threads] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Title] nvarchar(max)  NOT NULL,
    [CreateOn] datetime  NOT NULL,
    [Forum_Id] int  NOT NULL,
    [CreatedBy_Id] int  NOT NULL,
    [Category_Id] int  NOT NULL
);
GO

-- Creating table 'Posts'
CREATE TABLE [dbo].[Posts] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [CreatedOn] datetime  NOT NULL,
    [Thread_Id] int  NOT NULL,
    [CreatedBy_Id] int  NOT NULL
);
GO

-- Creating table 'SubForums'
CREATE TABLE [dbo].[SubForums] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [SubForum_1_Id] int  NULL,
    [Category_Id] int  NOT NULL
);
GO

-- Creating table 'Categories'
CREATE TABLE [dbo].[Categories] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL,
    [SortOrder] int  NOT NULL
);
GO

-- Creating table 'Users'
CREATE TABLE [dbo].[Users] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [DisplayName] nvarchar(max)  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'Threads'
ALTER TABLE [dbo].[Threads]
ADD CONSTRAINT [PK_Threads]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Posts'
ALTER TABLE [dbo].[Posts]
ADD CONSTRAINT [PK_Posts]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'SubForums'
ALTER TABLE [dbo].[SubForums]
ADD CONSTRAINT [PK_SubForums]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Categories'
ALTER TABLE [dbo].[Categories]
ADD CONSTRAINT [PK_Categories]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Users'
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [PK_Users]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [Forum_Id] in table 'Threads'
ALTER TABLE [dbo].[Threads]
ADD CONSTRAINT [FK_ForumThread]
    FOREIGN KEY ([Forum_Id])
    REFERENCES [dbo].[SubForums]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ForumThread'
CREATE INDEX [IX_FK_ForumThread]
ON [dbo].[Threads]
    ([Forum_Id]);
GO

-- Creating foreign key on [Thread_Id] in table 'Posts'
ALTER TABLE [dbo].[Posts]
ADD CONSTRAINT [FK_ThreadPost]
    FOREIGN KEY ([Thread_Id])
    REFERENCES [dbo].[Threads]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ThreadPost'
CREATE INDEX [IX_FK_ThreadPost]
ON [dbo].[Posts]
    ([Thread_Id]);
GO

-- Creating foreign key on [SubForum_1_Id] in table 'SubForums'
ALTER TABLE [dbo].[SubForums]
ADD CONSTRAINT [FK_SubForumSubForum]
    FOREIGN KEY ([SubForum_1_Id])
    REFERENCES [dbo].[SubForums]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_SubForumSubForum'
CREATE INDEX [IX_FK_SubForumSubForum]
ON [dbo].[SubForums]
    ([SubForum_1_Id]);
GO

-- Creating foreign key on [CreatedBy_Id] in table 'Posts'
ALTER TABLE [dbo].[Posts]
ADD CONSTRAINT [FK_UserPost]
    FOREIGN KEY ([CreatedBy_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserPost'
CREATE INDEX [IX_FK_UserPost]
ON [dbo].[Posts]
    ([CreatedBy_Id]);
GO

-- Creating foreign key on [CreatedBy_Id] in table 'Threads'
ALTER TABLE [dbo].[Threads]
ADD CONSTRAINT [FK_UserThread]
    FOREIGN KEY ([CreatedBy_Id])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_UserThread'
CREATE INDEX [IX_FK_UserThread]
ON [dbo].[Threads]
    ([CreatedBy_Id]);
GO

-- Creating foreign key on [Category_Id] in table 'SubForums'
ALTER TABLE [dbo].[SubForums]
ADD CONSTRAINT [FK_CategorySubForum]
    FOREIGN KEY ([Category_Id])
    REFERENCES [dbo].[Categories]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CategorySubForum'
CREATE INDEX [IX_FK_CategorySubForum]
ON [dbo].[SubForums]
    ([Category_Id]);
GO

-- Creating foreign key on [Category_Id] in table 'Threads'
ALTER TABLE [dbo].[Threads]
ADD CONSTRAINT [FK_CategoryThread]
    FOREIGN KEY ([Category_Id])
    REFERENCES [dbo].[Categories]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CategoryThread'
CREATE INDEX [IX_FK_CategoryThread]
ON [dbo].[Threads]
    ([Category_Id]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------